﻿Imports Capa_de_Acceso_a_Datos
Imports Capa_de_Negocio

Public Class frmDebitos

    Public FarmaciaId As Integer
    Public DebitoSeleccionado As Debito = New Debito()


    Private Sub Debitos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FarmaciaId = 0
        CargarCombos()
        CargarGrilla()

        With dgvDebitos
            ' alternar color de filas  
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FloralWhite
            .DefaultCellStyle.BackColor = Color.Beige
        End With

    End Sub

    Private Sub frmDebito_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub CargarCombos()
        'Combo Año
        cboAno.ValueMember = "AnoId"
        cboAno.DisplayMember = "Ano"
        cboAno.DataSource = Constante.obtenerAnos

        'Combo Mes 
        cboMes.ValueMember = "MesId"
        cboMes.DisplayMember = "Descripcion"
        cboMes.DataSource = MesDAO.comboFiltroMes
        'cboMesNC.SelectedValue = Date.Now.Month

        'Combo ObraSocialXPlan
        cboObraSocial.ValueMember = "ObraSocialXPlanId"
        cboObraSocial.DisplayMember = "Descripcion"
        cboObraSocial.DataSource = ObraSocialXPlanDAO.comboFiltroObraSocialXPlan

    End Sub

    Private Sub CargarGrilla()
        Try
            dgvDebitos.DataSource = DebitoDAO.obtenerDebitosXFiltro(cboObraSocial.SelectedValue, cboMes.SelectedValue, cboAno.SelectedValue, FarmaciaId)
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

        dgvDebitos.Refresh()
        'dgvDebitos.Columns("PresentacionAno").Width = 50
        'dgvDebitos.Columns("MesNombre").Width = 100
        'dgvDebitos.Columns("FarmaciaNombre").Width = 175
        'dgvDebitos.Columns("DebitoImporte").Width = 60
        'dgvDebitos.Columns("Descripcion").Width = 175

        'dgvDebitos.Columns("DebitoImporte").HeaderText = "Importe"
        'dgvDebitos.Columns("FarmaciaNombre").HeaderText = "Farmacia"
        'dgvDebitos.Columns("PresentacionAno").HeaderText = "Año"
        'dgvDebitos.Columns("MesNombre").HeaderText = "Mes"
        'dgvDebitos.Columns("Descripcion").HeaderText = "Obra Social"

        'dgvDebitos.Columns("DebitoId").Visible = False
        'dgvDebitos.Columns("DebitoMotivo").Visible = False
        'dgvDebitos.Columns("PresentacionId").Visible = False
        'dgvDebitos.Columns("FarmaciaId").Visible = False
        'dgvDebitos.Columns("MesId").Visible = False
        'dgvDebitos.Columns("ObraSocialXPlanId").Visible = False


    End Sub

    Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
        Me.Dispose()
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        frmSeleccionarFarmacia.frmLlamada = Me.Name
        frmSeleccionarFarmacia.ShowDialog()
    End Sub

    Private Sub btnFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFiltrar.Click
        CargarGrilla()
    End Sub

    Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        Me.Visible = False
        frmCargaDebitos.ShowDialog()
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click

        If HayFilaSeleccionada() Then
            cargarFilaSeleccionada()
            frmCargaDebitos.Modo = "Modificacion"
            Me.Visible = False
            frmCargaDebitos.ShowDialog()
        Else
            MsgBox("Seleccione primero una fila", MsgBoxStyle.Information, "Atención")
        End If
    End Sub

    Private Sub cargarFilaSeleccionada()
        DebitoSeleccionado.Id = dgvDebitos.SelectedRows(0).Cells("DebitoId").Value
        DebitoSeleccionado.Importe = dgvDebitos.SelectedRows(0).Cells("DebitoImporte").Value
        DebitoSeleccionado.Farmacia = dgvDebitos.SelectedRows(0).Cells("FarmaciaNombre").Value
        DebitoSeleccionado.Ano = dgvDebitos.SelectedRows(0).Cells("PresentacionAno").Value
        DebitoSeleccionado.Mes = dgvDebitos.SelectedRows(0).Cells("MesNombre").Value
        DebitoSeleccionado.FarmaciaId = dgvDebitos.SelectedRows(0).Cells("IdFarmacia").Value
        DebitoSeleccionado.ObraSocialXPlan = cboObraSocial.Text
        DebitoSeleccionado.PresentacionId = dgvDebitos.SelectedRows(0).Cells("PresentacionId").Value
        DebitoSeleccionado.Motivo = dgvDebitos.SelectedRows(0).Cells("DebitoMotivo").Value
    End Sub

    Private Function HayFilaSeleccionada() As Boolean
        Dim hay As Boolean = False

        If dgvDebitos.SelectedRows.Count > 0 Then
            hay = True
        End If

        Return hay
    End Function

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click

        If HayFilaSeleccionada() Then
            If MsgBox("¿Desea eliminar la nota de crédito seleccionada?", MsgBoxStyle.Question) = MsgBoxResult.Ok Then
                DebitoDAO.eliminarDebito(dgvDebitos.SelectedRows(0).Cells("DebitoId").Value)
                CargarGrilla()
            End If
        Else
            MsgBox("Seleccione primero una fila", MsgBoxStyle.Information, "Atención")
        End If
    End Sub

    Private Sub btnVisualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisualizar.Click

        If HayFilaSeleccionada() Then
            cargarFilaSeleccionada()
            frmCargaDebitos.Modo = "Visualizacion"
            Me.Visible = False
            frmCargaDebitos.ShowDialog()
        Else
            MsgBox("Seleccione primero una fila", MsgBoxStyle.Information, "Atención")
        End If
    End Sub

    Private Sub frmDebitos_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        CargarGrilla()
    End Sub

End Class